package com.jcxy.controller;

import com.jcxy.mapper.DictionaryMapper;
import com.jcxy.mapper.PositionMapper;
import com.jcxy.pojo.Dept;
import com.jcxy.pojo.Position;
import com.jcxy.service.PositionService;
import com.jcxy.util.PageBean;
import com.jcxy.util.ParamsDto;
import com.jcxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jobManage")
public class PositionController {

    @Autowired
    private PositionService positionService;
    @Autowired
    private DictionaryMapper dictionaryMapper;
    @Autowired
    private PositionMapper positionMapper;

    /**
     * 跳转到jobEdit.jsp页面
     * @param request
     * @return
     */
    @RequestMapping("toJobEditPage.do")
    public String toJobEditPage(HttpServletRequest request){

        //将参数转换成合适的map集合
        Map dto = ParamsDto.getDto(request.getParameterMap());
        List<Position> positionList = positionMapper.queryPosition(dto);

        request.setAttribute("job",positionList.get(0));
        request.setAttribute("pageType",1);

        return "userManage/jobEdit";
    }

    /**
     * 通过分页获得职位列表
     * @return
     */
    @RequestMapping("/getJobListByPage.do")
    @ResponseBody
    public ResponseData getJobListByPage(HttpServletRequest request){


        //将参数转换成合适的map集合
        Map dto = ParamsDto.getDto(request.getParameterMap());

        //调用业务逻辑层方法，查询角色列表
        PageBean pageBean = positionService.queryListByPage(
                dto,//查询条件
                Integer.valueOf(dto.get("page").toString()),//当前页
                Integer.valueOf(dto.get("limit").toString()));//页容量

        return pageBean.getResponseData();
    }

    /**
     * 获取部门列表信息
     * @return
     */
    @RequestMapping("/getDeptList.do")
    @ResponseBody
    public ResponseData getDeptList(Integer position_id){

        //用于存放发送到前端的数据
        List data = new ArrayList();

        //获取所有部门信息
        List<Dept> allDeptList = dictionaryMapper.queryDept();
        data.add(allDeptList);

        //判断职位id是否为空
        if(position_id != null){

            //获取职位所属的部门id
            List<Integer> deptIdList = positionMapper.queryDeptIdByJobId(position_id);
            data.add(deptIdList);
        }

        ResponseData ok = ResponseData.ok();
        ok.setData(data);
        return ok;
    }

    /**
     * 新增职位信息
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/addPosition.do")
    public String addPosition(HttpServletRequest request)throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        //插入职位信息
        positionService.insertPosition(dto);

        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "userManage/jobManage";
    }

    /**
     * 修改职位信息
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/updatePositionById.do")
    public String updatePositionById(HttpServletRequest request)throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        //更新职位信息
        positionService.updatePosition(dto);

        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "userManage/jobManage";
    }

    @RequestMapping("/deletePositionById.do")
    @ResponseBody
    public ResponseData deletePositionById(Integer position_id) throws Exception{

        positionService.deletePositionById(position_id);

        ResponseData ok = ResponseData.ok();
        return ok;
    }

}
